package com.zhisheng.utils;

import com.zhisheng.config.MyJwtProperties;
import io.jsonwebtoken.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;
import java.util.Properties;

@Component
public class MyUtilsJwt {
    @Autowired
    private MyJwtProperties properties;
    //两个方法 一个是生成token 一个是解析token
    public String getToken(Map<String, Object> map) {
        //生成令牌
        String s = Jwts.builder()
                .setClaims(map)
                .signWith(SignatureAlgorithm.valueOf(properties.getType()),
                        properties.getKey())
                .setExpiration(new Date( System.currentTimeMillis()+properties.getTtl()))
                .compact();
        return s;
    }
    public Map<String, Object> getClaims(String token) {
        //解析令牌
        Claims body = null;
        try {
            body = Jwts.parser()
                    .setSigningKey(properties.getKey())
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return body;
    }
}
